<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
  let S = 'aaaaaaab';
  let T = 'aab';
  function indexof(S,T) {
    let j = 0;
    let n = 0;
    let obj = {};
    obj.str = '';
    for (let i = 0; i < S.length; i++) {
      if(S[i] === T[j]) {
        obj.str += S[i];
        obj.position = i;
        j++;
        if(obj.str === T) {
          return i - T.length + 1;
        }
      } else {
        obj.str = '';
        j = 0;
        i = n++;
      }
    }
    if(obj.str !== T) {
      console.log('obj.str = ' + obj.str,'T = '+ T);
      return -1;
    }
  } 
  console.log(indexof(S,T));
  </script>
</body>
</html>